<?php

class edit_news
{
	var $mess;

	function edit_news()
	{
                global $style;
                $style->addJavascript('js/ajax.js');
                $style->addJavascript('js/admin_news.js');

		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select n_title from ". NEWSARTICLES_TABLE . " Where n_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". NEWSARTICLES_TABLE . " Where n_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['n_title'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$q = "Select * FROM " . CATAGORIES_TABLE . " WHERE c_newscat = 1 AND c_id = " . $_POST['catagory'] . " ORDER BY c_title ASC";
			$result = $db->sql_query($q);
			if($catagory = $db->sql_fetchrow($result)) 
			{
				$result = $db->sql_query(str_replace('{ID}', $_POST['owner'], $catagory['c_selectidquery']));
				if($item = $db->sql_fetchrow($result)) 
				{
					$ownername = $item[$catagory['c_selectname']];
					
					$qUpd = "insert into  ". NEWSARTICLES_TABLE . " (n_title, n_enabled, n_brief, n_full, n_owner, 
									n_catagory, n_posteddate, n_stickyexpiry, n_ownername) values('" . 
									$_POST['title'] . "', " . $_POST['enabled'] . ", '" . $_POST['brief'] . "', '" . 
									$_POST['full'] . "', " . $_POST['owner'] . ", " . $_POST['catagory'] . ", '" . 
									$_POST['posteddate'] . "', '" . $_POST['stickyexpiry'] . "', '" . $ownername . "')" ;

					$result = $db->sql_query($qUpd);
					
					$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
				}
				else
				{
					$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
				}
			}
			else
			{
				$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			}
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$q = "Select * FROM " . CATAGORIES_TABLE . " WHERE c_newscat = 1 AND c_id = " . $_POST['catagory'] . " ORDER BY c_title ASC";
			$result = $db->sql_query($q);
			if($catagory = $db->sql_fetchrow($result)) 
			{
				$result = $db->sql_query(str_replace('{ID}', $_POST['owner'], $catagory['c_selectidquery']));
				if($item = $db->sql_fetchrow($result)) 
				{
					$ownername = $item[$catagory['c_selectname']];
					
					$qUpd = "update  ". NEWSARTICLES_TABLE . " set n_title = '" . $_POST['title'] .
															  "', n_brief = '" . $_POST['brief'] .
															  "', n_full= '" . $_POST['full'] .
															  "', n_posteddate= '" . $_POST['posteddate'] .
															  "', n_stickyexpiry= '" . $_POST['stickyexpiry'] .
															  "', n_enabled = " . $_POST['enabled'] . 
															  ", n_owner = " . $_POST['owner'] . 
															  ", n_catagory = " . $_POST['catagory'] . 
															  ", n_ownername = '" . $ownername .
															  "' WHERE n_id = " . $_POST['id'] ;
					$result = $db->sql_query($qUpd);
					
					$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
				}
				else
				{
					$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
				}
			}
			else
			{
				$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			}
		
		
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		$valid = $valid && is_numeric($_POST['catagory']) && is_numeric($_POST['owner']) && is_numeric($_POST['enabled']);
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". NEWSARTICLES_TABLE . " Where n_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
					
			$row = formatArrayOutput($row, true);
			
			$catagoryOptions = '';
			$catagory = '';
			$q = "Select * FROM " . CATAGORIES_TABLE . " WHERE c_newscat = 1 ORDER BY c_title ASC";
			$result = $db->sql_query($q);
			while($cat = $db->sql_fetchrow($result)) 
			{
				if($cat['c_id'] == $row['n_catagory'])
				{
					$catagory = $cat;
					$catagoryOptions .= '<option value="' . $cat['c_id'] . '" SELECTED>' . $cat['c_title'] . '</option>';
				}
				else
				{
					$catagoryOptions .= '<option value="' . $cat['c_id'] . '">' . $cat['c_title'] . '</option>';
				}
			}
				
			$catagoryItems = '';
			$result = $db->sql_query($catagory['c_selectquery']);
			while($item = $db->sql_fetchrow($result)) 
			{
				if($item[$catagory['c_selectid']] == $row['n_owner'])
					$catagoryItems .= '<option value="' . $item[$catagory['c_selectid']] . '" SELECTED>' . 
									$item[$catagory['c_selectname']] . '</option>';
				else
					$catagoryItems .= '<option value="' . $item[$catagory['c_selectid']] . '">' . 
									$item[$catagory['c_selectname']] . '</option>';
				
			}
		
			$enabledOptions = '<option value="0"'. ($row['n_enabled']==0 ? 'SELECTED' : '').'>No</option>' .
							 '<option value="1"'. ($row['n_enabled']==1 ? 'SELECTED' : '').'>Yes</option>';
							 
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
			
			include $skin_dir . 'admin/editnewsarticle.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir,  $lang, $db;
		
		
		$catagoryOptions = '';
		$catagory = '';
		$q = "Select * FROM " . CATAGORIES_TABLE . " WHERE c_newscat = 1 ORDER BY c_title ASC";
		$result = $db->sql_query($q);
		while($cat = $db->sql_fetchrow($result)) 
		{
			if($catagory == '')
			{
				$catagory = $cat;
			}
			$catagoryOptions .= '<option value="' . $cat['c_id'] . '">' . $cat['c_title'] . '</option>';
		}
			
		$catagoryItems = '';
		$result = $db->sql_query($catagory['c_selectquery']);
		while($item = $db->sql_fetchrow($result)) 
		{
			$catagoryItems .= '<option value="' . $item[$catagory['c_selectid']] . '">' . 
								$item[$catagory['c_selectname']] . '</option>';
		}
			
		
			
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$enabledOptions = '<option value="0" SELECTED>No</option>' .
						 '<option value="1">Yes</option>';
						 
		$row['n_id'] = '';
		$row['n_title'] = '';
		$row['n_enabled'] = '';
		$row['n_full'] = '';
		$row['n_brief'] = '';
		$row['n_owner'] = '';
		$row['n_catagory'] = '';
		$row['n_stickyexpiry'] = '';
		$row['n_posteddate'] = '';
		
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/editnewsarticle.tpl';
	}
	
}
?>